package lib

func init() {
	Probs = append(Probs, Problem{
		Num:         20,
		Discription: "有效的括号",
		Level:       1,
		Labels: map[string]int{
			"栈":   1,
			"哈希表": 1,
		},
	})
}

func IsValid(s string) bool {
	matchMap := map[byte]byte{
		')': '(',
		']': '[',
		'}': '{',
	}
	stack := make([]byte, 0)
	for i := range s {
		ch := s[i]
		if ch == '(' || ch == '[' || ch == '{' {
			stack = append(stack, ch)
			continue
		}

		if len(stack) == 0 || stack[len(stack)-1] != matchMap[ch] {
			return false
		}

		stack = stack[:len(stack)-1]
	}

	return len(stack) == 0
}